<?php
namespace Home\Controller;
use Think\Controller;
class GoodsController extends Controller {
    public function index(){
        $id = I('id', 0);
        if(!$id) return $this->error('商品ID不正确', U('Index/index'));
        $goods_info = M('goods')->find($id);
        if(!$goods_info || !$goods_info['status']) return $this->error('商品信息不正确', U('Index/index'));

        $platform_arr = M('platform')->where('status = 1')->getField('id, name');
        $goods_img    = $this->get_goods_img($id);

        $drawer_info       = M('drawer')->find($goods_info['drawer_id']);
        $drawer_goods_list = $this->get_drawer_goods_by_id($goods_info['drawer_id']);
        $seo           = array(
            'title'       => $goods_info['title'],
            'keywords'    => $goods_info['title'],
            'description' => $goods_info['title']
        );

        //只获取商品前10条评论
        $comment_list = $this->get_comment($id);

        //获得用户的抽屉，在登陆的情况下
        $user_id = is_login();
        if($user_id) {
            $drawer_list = D('drawer')->get_user_drawer_list($user_id, 'id, title');
            $this->assign('drawer_list', $drawer_list);
        }

        $this->assign('seo', $seo);
        $this->assign('comment_list', $comment_list);
        $this->assign('platform', $platform_arr);
        $this->assign('is_followed', $this->is_followed_drawer($goods_info['drawer_id']));
        $this->assign('goods_img', $goods_img);
        $this->assign('goods_info', $goods_info);
        $this->assign('drawer_goods_list', $drawer_goods_list);
        $this->assign('drawer_info', $drawer_info);
        $this->display();
    }

    protected function get_goods_img($goods_id)
    {
        $map = array('status' => 1, 'goods_id' => $goods_id);
        $goods_img = M('goods_image')->where($map)->select();
        return $goods_img;
    }

    /**
     * 获得用户是否关注某个抽屉
     * @param $drawer_id
     * @return bool
     */
    private function is_followed_drawer($drawer_id)
    {
        $user_id = is_login();
        if (!$user_id || !$drawer_id) return false;

        $filter = array('drawer_id' => $drawer_id, 'uid' => $user_id, 'status' => 1);
        $follow_info = M('drawer_follow_record')->where($filter)->find();
        return $follow_info ? true : false;
    }

    /**
     * 获取导航数据, @todo 将导航数据写入memcache
     */
    private function get_nav() {
        $cate_model = M('category');
        $cate_data = $cate_model->order('cate_path')->select();
        return $this->get_category($cate_data, 0, 2);
    }


    /**
     * 添加商品评论
     * @todo 是不是需要添加验证码或者评论次数限制或者次数限制，暂时先不考虑，后期再添加
     * @param $goods_id 商品ID
     * @param $uid      用户ID
     * @param $comment  评论内容
     * @param $reply_id 回复的评论ID
     */
    public function add_comment()
    {
        $goods_id = I('goods_id', 0);
        $uid      = is_login();
        $comment  = I('comment', '');
        $reply_id = I('reply_id', 0);

        $res = array('status' => 'error', 'msg' => '', 'redirect_url' => '');
        if (!$goods_id) {
            $res['msg'] = '商品ID不存在';
            $this->ajaxReturn($res);
        }
        $goods_info = M('goods')->where(array('status' => 1))->find($goods_id);
        if (!$goods_info) {
            $res['msg'] = '商品信息不存在或已删除';
            $this->ajaxReturn($res);
        }

        if (!$uid) {
            $uid = is_login();
        }

        if (!$uid) {
            $res['msg'] = '用户id不存在';
            $res['redirect_url'] = U('User/login');
            $this->ajaxReturn($res);
        }

        if (!$comment) {
            $res['msg'] = '评论内容不能为空';
            $this->ajaxReturn($res);
        }

        if ($reply_id) {
            $reply_info = M('goods_comment')->where(array('status' => 1))->find($reply_id);
            if (!$reply_id) {
                $res['msg'] = '评论信息不存在';
                $this->ajaxReturn($res);
            }
        }

        $info = array(
            'goods_id' => $goods_id,
            'uid'      => $uid,
            'comment'  => $comment,
            'reply_id' => $reply_id,
            'add_time' => date('Y-m-d H:i:s')
        );
        $comment_id = M('goods_comment')->add($info);
        if (!$comment_id) {
            $res['msg'] = '添加商品评论失败';
            $this->ajaxReturn($res);
        }

        $res['status'] = 'succ';
        $res['msg']    = '添加评论成功';
        $this->ajaxReturn($res);
    }


    public function get_comment($goods_id, $start = 0, $num = 10)
    {
        $res = array('status' => 'error', 'msg' => '', 'redirect' => '');
        $goods_info = M('goods')->where(array('status' => 1))->find($goods_id);
        if (!$goods_info) {
            $res['msg'] = '商品信息不存在';
            $this->auto_return($res);
        }

        //按照顶的数量倒序， 发表时间也倒序
        $comment_list = M('goods_comment')->where(array('goods_id' => $goods_id, 'status' => 1))
                                          ->order('up_count DESC, add_time DESC')
                                          ->limit($start, $num)
                                          ->select();

        //获得评论的用户ID和头像等内容
        if (count($comment_list)) {
            foreach ($comment_list as $k=>$v) {
                $user_info = M('user')->where('id ='.$v['uid'])->find();
                $comment_list[$k]['user_name'] = $user_info['user_name'];
                $comment_list[$k]['avatar']    = $user_info['avatar'];
                $comment_list[$k]['time_past'] = time_past($v['add_time']);

                if($v['reply_id']) {
                    $reply_info = M('goods_comment')->where('id ='.$v['reply_id'])->find();
                    $reply_user_info = M('user')->where('id ='.$reply_info['uid'])->find();
                    $comment_list[$k]['reply_user_name'] = $reply_user_info['user_name'];
                    $comment_list[$k]['reply_content']   = $reply_info['comment'];
                }
            }
        }

        return $this->auto_return($comment_list);
    }

    //评论点赞
    public function approve()
    {
        $result = array('status' => 'error', 'msg'=>'');
        $user_id = is_login();
        if(!$user_id) {
            $result['msg'] = '您还没有登录，请先登录';
            $this->ajaxReturn($result);
        }

        $comment_id = I('comment_id', 0);
        if (!$comment_id) {
            $result['msg'] = '评论ID不存在';
            $this->ajaxReturn($result);
        }

        $comment_info = M('goods_comment')->where('id='.$comment_id)->find();
        if (!$comment_info) {
            $result['msg'] = '评论信息不存在';
            $this->ajaxReturn($result);
        }

        $filter = array('comment_id' => $comment_id, 'uid' => $user_id, 'status' => 1);
        $comment_approve_info = M('comment_approve_record')->where($filter)->find();
        if ($comment_approve_info) {
            $result['msg'] = '您已经称赞过了';
            $this->ajaxReturn($result);
        }

        $info = array('comment_id' => $comment_id, 'uid' => $user_id, 'add_time'=>date('Y-m-d H:i:s'));
        $approve_res = M('comment_approve_record')->add($info);
        if (!$approve_res){
            $result['msg'] = '称赞失败';
            $this->ajaxReturn($result);
        }

        //将评论的up_count加1
        M('goods_comment')->where('id='.$comment_id)->setInc('up_count', 1);
        $result = array('status' => 'succ', 'msg' => '称赞成功');
        $this->ajaxReturn($result);
    }

    //举报评论
    public function report()
    {
        $result = array('status' => 'error', 'msg'=>'');
        $user_id = is_login();
        if(!$user_id) {
            $result['msg'] = '您还没有登录，请先登录';
            $this->ajaxReturn($result);
        }

        $comment_id = I('comment_id', 0);
        if (!$comment_id) {
            $result['msg'] = '评论ID不存在';
            $this->ajaxReturn($result);
        }

        $comment_info = M('goods_comment')->where('id='.$comment_id)->find();
        if (!$comment_info) {
            $result['msg'] = '评论信息不存在';
            $this->ajaxReturn($result);
        }

        $filter = array('comment_id' => $comment_id, 'uid' => $user_id);
        $comment_approve_info = M('comment_report')->where($filter)->find();
        if ($comment_approve_info) {
            $result['msg'] = '您已经举报过了';
            $this->ajaxReturn($result);
        }

        $info = array('comment_id' => $comment_id, 'uid' => $user_id, 'add_time'=>date('Y-m-d H:i:s'));
        $approve_res = M('comment_report')->add($info);
        if (!$approve_res){
            $result['msg'] = '举报失败';
            $this->ajaxReturn($result);
        }

        //将评论举报的审核放在后台，不能已举报就让评论失效
        $result = array('status' => 'succ', 'msg' => '举报成功，感谢您的举报');
        $this->ajaxReturn($result);
    }


    /**
     * 只能根据请求方式来返回对应的结果，ajax就使用 ajaxReturn 返回数据，普通的就return
     * @param $info
     */
    private function auto_return($info)
    {
        if (IS_AJAX) {
            $this->ajaxReturn($info);
        } else {
            return $info;
        }
    }

    /**
     * 通过一件商品的id获得其所属的抽屉的所有商品信息
     * @param $goods_id
     */
    private function get_drawer_goods_by_goods_id($goods_id)
    {
        $drawer_id = M('goods')->getField('drawer_id')->find($goods_id);

        $filter = array('drawer_id' => $drawer_id, 'status' => 1);
        $goods_list = M('goods')->where($filter)->field('id, title, cover')->select();
        return $goods_list;
    }


    /**
     * 通过抽屉id获得该抽屉下的所有商品
     * @param $drawer_id
     */
    private function get_drawer_goods_by_id($drawer_id)
    {
        $filter = array('drawer_id' => $drawer_id, 'status' => 1);
        $goods_list = M('goods')->where($filter)->field('id, title, cover')->select();
        return $goods_list;
    }

    /**
     * 获取导航无限极分类信息
     * @param $data 数据库中存放的分类数据
     * @param $pid  父级id
     * @param $layer 层级，表示要从中遍历出多少层的分类数据 1表示只取一层
     */
    private function get_category($data=array(), $pid=0, $layer)
    {
        $result = array();
        static $loop_count = 0;
        if($loop_count == $layer) return $result;

        foreach($data as $k=>$v) {
            if($v['pid'] == $pid) {
                //只有获取的数据超过一层的时候采取获取下层的数据
                if($layer > 1) {
                    $v[child] = $this->get_category($data, $v['id'], $loop_count+1);
                }
                $result[] = $v;
            }
        }
        $loop_count++;
        return $result;
    }
}